/*******************************************************************************
 // Copyright (c) Microsoft Corporation.
 // All rights reserved.
 //
 // This code is licensed under the MIT License.
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files(the "Software"), to deal
 // in the Software without restriction, including without limitation the rights
 // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
 // copies of the Software, and to permit persons to whom the Software is
 // furnished to do so, subject to the following conditions :
 //
 // The above copyright notice and this permission notice shall be included in
 // all copies or substantial portions of the Software.
 //
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 ******************************************************************************/
package com.sso.oauth.controller;

import com.microsoft.aad.adal4j.AuthenticationResult;
import com.sso.oauth.constants.AuthParameterNames;
import com.sso.oauth.utils.SessionHelper;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@Log4j
public class AadController {
    @Autowired
    private SessionHelper sessionHelper;

    @RequestMapping(value = "/secure/loginhandler",method = {RequestMethod.GET, RequestMethod.POST})
    public String getDirectoryObjects(ModelMap model, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        log.info("【Get Response From Azure】");
        HttpSession session = httpRequest.getSession();

        AuthenticationResult result = (AuthenticationResult) session.getAttribute(AuthParameterNames.PRINCIPAL_SESSION_NAME);
        if (result == null) {
            log.error("【AuthenticationResult not found in session.】");
            model.addAttribute("error", new Exception("AuthenticationResult not found in session."));
            return "/error";
        }

        try {
            sessionHelper.processUserLogin(httpRequest, result);
            String redirectPagePath = (String) session.getAttribute(AuthParameterNames.GOTO_PATH);
            log.info("【Redirect Original Page】 path = " + redirectPagePath);
            httpResponse.sendRedirect(redirectPagePath);
            return null;
        } catch (Exception e) {
            log.error("【Handle Login Message Failed】", e);
            return "/error";
        }
    }

    /**
     * 提供给Azure AD的注销登录回调
     * @return
     */
    @RequestMapping(value = "/logouthandler",method = {RequestMethod.GET, RequestMethod.POST})
    public String logoutHandler(HttpServletRequest request){

        sessionHelper.removeLoginSessionInfo(request);

        return "/index";
    }





}
